﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;

namespace RubricaSentimentale.Model
{
    [DataContract]
    [KnownType(typeof(FormattedList<string>))]
    [KnownType(typeof(FormattedList<ImportantDate>))]
    public class Persona:Notificable
    {
        public enum DetailType{FILM,FOOD,DATE,TEXT}


        private string _nome;
        [DataMember]
        public string Nome
        {
            get { return _nome; }
            set {
                _nome = value;
                notify("Nome");
            }
        }

        private string _cognome;
        [DataMember]
        public string Cognome
        {
            get { return _cognome; }
            set {
                _cognome = value;
                notify("Cognome");
            }
        }

        private string img = "";
        [DataMember]
        public string ImgSrc {
            get {
                    return img;
            }
            set {
                if (value != null && value != "")
                {
                    img = value;
                    BitmapImage bi = new BitmapImage();
                    IsolatedStorageFileStream str = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(value, System.IO.FileMode.Open);
                    bi.SetSource(str);
                    str.Close();
                    Image = bi;
                }
                else
                {
                    BitmapImage bi = new BitmapImage(new Uri("/Resources/default.png",UriKind.Relative));
                    Image = bi;
                }
            }
        }
        private ImageSource _image;
        [IgnoreDataMember]
        public ImageSource Image {
            get { return _image; }
            set {

                _image = value;
                notify("Image");
            }
        }

        private MyString _note = "";
        [DataMember]
        public MyString Note {
            get { return _note; }
            set {
                _note = value;
                notify("Note");
            }
        }

        private FormattedList<MyString> _films = new FormattedList<MyString>();
        [DataMember]
        public FormattedList<MyString> Films {
            get {
                return _films;
            }
            set {
                _films = value;
                notify("Films");
            }
        }

        private FormattedList<MyString> _food = new FormattedList<MyString>();
        [DataMember]
        public FormattedList<MyString> Food
        {
            get
            {
                return _food;
            }
            set
            {
                _food = value;
                notify("Food");
            }
        }

        private FormattedList<ImportantDate> _date = new FormattedList<ImportantDate>();
        [DataMember]
        public FormattedList<ImportantDate> Date {
            get { return _date; }
            set {
                _date = value;
                notify("Date");
            }
        }

        public Persona(string nome, string cognome)
        {
            this.Nome = nome;
            this.Cognome = cognome;
            Initdetails();
            BitmapImage a = new BitmapImage(new Uri("Resources/default.png", UriKind.Relative));
            Image = a;
        }


        public void Initdetails()
        {
            Films.Intestazione = "FILM PREFERITI";
            Date.Intestazione = "DATE IMPORTANTI";
            Food.Intestazione = "CIBI PREFERITI";
        }


        /// <summary>
        /// Function that return true if the key is completely or partially contained in name, surname or both
        /// </summary>
        /// <param name="key">Key value to find(Ex. "marco rossi","marc","Marco Ross")</param>
        /// <returns></returns>
        public bool Contains(string key)
        {
            string namesurname = (Nome + " " + Cognome).ToLower();
            string surnamename = (Cognome + " " + Nome).ToLower();
            key = key.ToLower();
            return namesurname.StartsWith(key) || surnamename.StartsWith(key);
        }


        public void AddDetail(DetailType type, object value)
        {
            switch (type)
            { 
                case DetailType.TEXT:
                    this.Note = (string)value;
                    break;
                case DetailType.FILM:
                    this.Films.Add((string)value);
                    break;
                case DetailType.FOOD:
                    this.Food.Add((string)value);
                    break;
                case DetailType.DATE:
                    this.Date.Add((ImportantDate)value);
                    break;
                default:
                    break;
            }
        }



        public void RemoveDetail(DetailType type, object value)
        {
            switch (type)
            {
                case DetailType.TEXT:
                    this.Note = "";
                    break;
                case DetailType.FILM:
                    this.Films.Remove((string)value);
                    break;
                case DetailType.FOOD:
                    this.Food.Remove((string)value);
                    break;
                case DetailType.DATE:
                    this.Date.Remove((ImportantDate)value);
                    break;
                default:
                    break;
            }
        }



        public void ModifyDetail(DetailType type,object old, object value)
        {
            switch (type)
            {
                case DetailType.TEXT:
                    this.Note = (string)value;
                    break;
                case DetailType.FILM:
                    if (this.Films.Contains((string)value))
                        Films[Films.IndexOf((string)old)] = (string)value;
                    else
                        this.Films.Add((string)value);
                    break;
                case DetailType.FOOD:
                    this.Food.Add((string)value);
                    break;
                case DetailType.DATE:
                    this.Date.Add((ImportantDate)value);
                    break;
                default:
                    break;
            }
        }


    }
}
